package com.foreknow.elm.controller;

import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.service.impl.CartServiceImpl;
import com.foreknow.elm.service.impl.OrdersServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


public class OrdersController {
    /**
     * 根据用户编号、商家编号、订单总金额、送货地址编号向订单表中添加一条记录，
     * 并获取自动生成的订单编号，
     * 然后根据用户编号、商家编号从购物车表中查询所有数据，批量添加到订单明细表中，
     * 然后根据用户编号、商家编号删除购物车表中的数据。
     * @param request
     * @return
     */
    public int createOrders(HttpServletRequest request){
        OrdersService service = new OrdersServiceImpl();
        String userId = request.getParameter("userId");
        int businessId = Integer.parseInt(request.getParameter("businessId"));
        double orderTotal = Double.parseDouble(request.getParameter("orderTotal"));
        int daId = Integer.parseInt(request.getParameter("daId"));
        return service.createOrders(userId,businessId,orderTotal,daId);
    }

    /**
     * 根据订单编号查询订单信息，包括所属商家信息，和此订单的所有订单明细信息
     */
    public Orders getOrdersById(HttpServletRequest request){
        OrdersServiceImpl ordersService = new OrdersServiceImpl();
        int orderId = Integer.parseInt(request.getParameter("orderId"));
        return ordersService.getOrdersById(orderId);
    }

    /**
     * 根据用户编号查询此用户的所有订单信息
     */
    public List<Orders> listOrdersByUserId(HttpServletRequest request){
        OrdersServiceImpl ordersService = new OrdersServiceImpl();
        String userId = request.getParameter("userId");
        return ordersService.listOrdersByUserId(userId);
    }


}
